<template>
  <div class="role-container">
    <div class="left-wrapper">
      <div v-for="(item, index) in roleList" :key="item.roleId" class="role-item"
        :class="{ active: index === activeIndex }" @click="changeRole(index)">
        <div class="role-info">
          <svg-icon :icon-class="index === activeIndex ? 'user-active' : 'user'" class="icon" />
          {{ item.roleName }}
        </div>
        <div class="more">
          <el-dropdown trigger="click" @command="handleCommand($event, item)">
            <span class="el-dropdown-link">
              <svg-icon icon-class="more" />
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="edit">编辑角色</el-dropdown-item>
                <el-dropdown-item command="delete">删除角色</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
      <el-button class="addBtn" size="mini" @click="$router.push('/roleAdd')">添加角色</el-button>
    </div>
    <div class="right-wrapper">
      <el-tabs v-model="activeName">
        <el-tab-pane label="功能权限" name="tree">
          <div class="tree-wrapper">
            <div v-for="item in treeList" :key="item.id" class="tree-item">
              <div class="tree-title"> {{ item.title }} </div>
              <el-tree ref="tree" :data="item.children" show-checkbox :default-expand-all="true" node-key="id"
                :props="{ label: 'title' }" />
            </div>
          </div>
        </el-tab-pane>
        <el-tab-pane :label="`成员(${roleUserList.length})`" name="user">
          <div class="user-wrapper">
            <el-table :data="roleUserList" style="width: 100%">
              <el-table-column type="index" width="250" label="序号" />
              <el-table-column prop="name" label="员工姓名" />
              <el-table-column prop="userName" label="登录账号" />
            </el-table>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>
<script>
import { getRoleListAPI, getTreeListAPI, getRoleDetailAPI, getRoleUserAPI, delRoleUserAPI } from '@/api/role'
function addDisabled(treeList) {
  treeList.forEach(item => {
    item.disabled = true
    if (item.children) {
      addDisabled(item.children)
    }
  })
}
export default {
  name: 'Role',
  data() {
    return {
      roleList: [],
      activeIndex: 0,
      treeList: [],
      perms: [],
      activeName: 'tree',
      roleUserList: []
    }
  },
  methods: {
    async getRoleList() {
      const res = await getRoleListAPI()
      // console.log(res)
      this.roleList = res.data
    },
    changeRole(index) {
      this.activeIndex = index
      const roleId = this.roleList[index].roleId
      this.getRoleDetail(roleId)
      this.getRoleUserList(roleId)
      console.log(roleId)
    },
    async getTreeList() {
      const res = await getTreeListAPI()
      // console.log(res);
      this.treeList = res.data
      addDisabled(this.treeList)

    },
    async getRoleDetail(roleId) {
      const res = await getRoleDetailAPI(roleId)
      this.perms = res.data.perms
      this.$refs.tree.forEach((item, index) => {
        item.setCheckedKeys(this.perms[index])
      })
    },
    async getRoleUserList(roleId) {
      const res = await getRoleUserAPI(roleId)
      this.roleUserList = res.data.rows
    },
    handleCommand(command, role) {
      if (command === 'edit') {
        this.$router.push(`/roleEdit/${role.roleId}`)
      } else if (command === 'delete') {
        this.$confirm('确认删除该角色吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          await delRoleUserAPI(role.roleId)
          this.$message.success('删除成功')
          // 重新获取角色列表
          await this.getRoleList()
          // 如果删除的是当前选中的角色，重新选择第一个角色
          if (this.activeIndex >= this.roleList.length) {
            this.activeIndex = 0
          }
          // 更新右侧面板数据
          if (this.roleList.length > 0) {
            const roleId = this.roleList[this.activeIndex].roleId
            await this.getRoleDetail(roleId)
            await this.getRoleUserList(roleId)
          }
        }).catch(() => {
          this.$message.info('已取消删除')
        })
      }
    }
  },
  async mounted() {
    await this.getRoleList()
    await this.getTreeList()
    this.getRoleDetail(this.roleList[0].roleId)
    this.getRoleUserList(this.roleList[0].roleId)
  }
}
</script>

<style scoped lang="scss">
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;
  padding: 20px;

  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
      }
    }

    .more {
      display: flex;
      align-items: center;
      cursor: pointer;
      
      .el-dropdown-link {
        display: flex;
        align-items: center;
      }
    }

    .addBtn {
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;

        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper {
      padding: 20px;
    }

    ::v-deep .el-tree-node__label {
      color: rgba(0, 0, 0, 0.3);
    }

    ::v-deep .el-tree-node__content label.el-checkbox.is-checked {
      span.is-checked .el-checkbox__inner {
        background-color: #0094ff;
      }
    }
  }
}
</style>
